/*
*
* 工作台公共方法
*   @params:
*     item -> item当前数据
*     index -> item当前索引
*     data -> data列表数据
*     _this -> 当前vue组件
*       config -> 当前方法参数
*
* */
import {checkuserstat, doworkflow, updatestate, getworkflowrecord} from '@/api/examine.js'
import {plan_getlist, plan_delete, purchase_delete, programma_delete, contract_delete} from '@/api/work_bench.js'
import {MessageBox} from 'mint-ui'

// 查看是具有审批功能
export async function vmCommonCheckExamine(item, index, data, _this, config) {
  const item_id = item[config.id];
  const item_flag = item[config.flag];
  const item_index = index;
  _this.tempListData = item;
  _this.tempIndex = index;
  const {item_objType, item_opinion, item_description} = config;
  switch (item_objType) {
    // 合同审批
    case 'contract':
      if (item_flag == '2') {
        _this.$toast('您已审核通过！');
        _this.$refs.swipeoutItem[item_index].close();
        return;
      } else if (item_flag == '3') {
        _this.$toast('已作废，不能审批！');
        _this.$refs.swipeoutItem[item_index].close();
        return;
      } else if (item_flag == '4') {
        _this.$toast('审核驳回，不能审批！');
        _this.$refs.swipeoutItem[item_index].close();
        return;
      }
      break;
    case 'requirement':
      if (item_flag == '1') {
        _this.$toast('审核通过无法操作');
        _this.$refs.swipeoutItem[item_index].close();
        return;
      }
      break;
    case 'arrivalorder':
      if (item_flag == '1') {
        _this.$toast('您已审核通过！');
        _this.$refs.swipeoutItem[item_index].close();
        return;
      }
      break;
    case 'waitingarea':
      if (item_flag == '1') {
        _this.$toast('您已审核通过！');
        _this.$refs.swipeoutItem[item_index].close();
        return;
      }
      break;
    case 'qualityinspection':
      if (item_flag == '1') {
        _this.$toast('您已审核通过！');
        _this.$refs.swipeoutItem[item_index].close();
        return;
      }
      break;
    case 'returnslip':
      if (item_flag == '1') {
        _this.$toast('您已审核通过！');
        _this.$refs.swipeoutItem[item_index].close();
        return;
      }
      break;
  }
  let isStatus = await checkuserstat(item_objType, item_id).then(res => res.data);
  console.log(isStatus);
  if (isStatus == 1) {
    _this.examineStatus = !_this.examineStatus;
  } else {
    _this.$toast(isStatus);
    _this.$refs.swipeoutItem[index].close();
  }
}

// 审批改变状态
export async function vmCommondoworkflow(data, _this, config) {
  const item_id = _this.tempListData[config.id];
  const isActive1 = _this.isActive1;
  const isActive2 = _this.isActive2;
  const item_index = _this.tempIndex;
  const {item_objType} = config;
  let opinion;
  let description = _this.reasonData;
  if (!_this.isActive1 && !_this.isActive2) {
    _this.$toast('请选择是否通过审批！');
    return;
  } else {
    opinion = _this.isActive1 ? 1 : 2;
  }
  let res = await doworkflow(item_objType, item_id, opinion, description).then(res => res);
  if (res.data.data == 1 && res.data.status == 200) {
    await updatestate(item_objType, item_id).then(res => {
      if (res.data.data == 1 && res.data.status == 200) {
        switch (item_objType) {
          // 合同审批
          case 'contract':
            data[item_index][config.flag] = 2;
            break;
          // 需求计划管理
          case 'requirement':
            data[item_index][config.flag] = 1;
            break;
          // 采购计划管理
          case 'purchase':
            data[item_index][config.flag] = 1;
            break;
          // 到货单
          case 'arrivalorder':
            data[item_index][config.flag] = 1;
            break;
          // 待检单
          case 'waitingarea':
            data[item_index][config.flag] = 1;
            break;
          //  质检单
          case 'qualityinspection':
            data[item_index][config.flag] = 1;
            break;
          // 退货单
          case 'returnslip':
            data[item_index][config.flag] = 1;
            break;
          // 仓库盘点
          case 'inventory':
            data[item_index][config.flag] = 1;
            break;
          case 'transfer':
            data[item_index][config.flag] = 1;
            break;
          // 谈判列表
          case 'negotiate':
            data[item_index][config.flag] = 1;
            break;
          // 单一来源
          case 'single':
            data[item_index][config.flag] = 1;
            break;
          //  询价列表
          case 'consultprice':
            data[item_index][config.flag] = 1;
            break;
          // 询价比
          case 'bidding':
            data[item_index][config.flag] = 1;
            break;
          // 审核公告
          case 'invitebids':
            data[item_index][config.flag] = 1;
            break;
          case 'sendbidslist':
            data[item_index][config.flag] = 1;
            break;
        }
        _this.$toast(res.data.message);
        _this.examineStatus = false;
      } else {
        _this.$toast(res.data.message);
        _this.examineStatus = false;
      }
    })
  } else if (res.data.data == 2 && res.data.status == 200) {
    _this.$toast('操作成功！');
    _this.examineStatus = false;
  } else {
    _this.$toast('操作失败！');
    _this.examineStatus = false;
  }
}

// 删除功能
export function vmCommonDeleteItem(item, index, data, _this, config) {
  let deleteAPI;
  switch (config.item_deleteType) {
    // 需求计划删除接口
    case 'plan':
      deleteAPI = plan_delete;
      break;
    //  采购计划删除接口
    case 'purchase':
      deleteAPI = purchase_delete;
      break;
    // 采购方案删除接口
    case 'programme':
      deleteAPI = programma_delete;
      break;
    case 'contract':
      deleteAPI = contract_delete;
      break;
  }
  MessageBox.confirm('确定是否删除该条记录？').then(action => {
    deleteAPI(item[config.id]).then(res => {
      console.log(res);
      if (res.status == 200 && res.data.status != 500) {
        _this.$toast('删除成功');
        _this.$refs.swipeoutItem[index].close();
        data.splice(index, 1);
      } else {
        let msg;
        if (res.data && !res.data.message) {
          msg = res.data.message;
        } else {
          msg = '操作失败！';
        }
        _this.$toast(msg);
        _this.$refs.swipeoutItem[index].close();
      }
    })
  }).catch(() => {
    _this.$refs.swipeoutItem[index].close();
  });
}

// 查看审批流
export class VmCommonExamineProcess {
  constructor(item, key, that, config) {
    this.that = that;
    this.item = item;
    this.key = key;
    this.config = config;
    this.id = config.id;
    this.oid = config.oId;
    this.objType = this.config.objType;
    this.init();
  }
  init() {
    this.getworkflowData();
  }
  async getworkflowData() {
    let id;
    if (this.id) {
      id = this.item[this.id];
    } else {
      id = this.oid;
    }
    const objType = this.objType;
    getworkflowrecord(objType, id).then(res => {
      if ( !res.data.data ) {
        this.that.$toast('该数据流程为空！');
        this.that.closePopup();
        return;
      }
      this.that.popupVisible = !this.that.popupVisible;
      let tempArr = [];
      for (let o in res.data.data) {
        let tempObj = {};
        tempObj.code = o;
        tempObj.data = res.data.data[o];
        tempArr.push(tempObj);
        tempObj = null;
      }
      this.that.commonExamineProcess = tempArr;
    });
  }
}
